package com.renrenyou.duanshipin.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.renrenyou.duanshipin.R;
import com.renrenyou.duanshipin.base.BaseActivity;
import com.renrenyou.duanshipin.databinding.ActivityMainBinding;
import com.renrenyou.duanshipin.net.Constance;
import com.renrenyou.duanshipin.util.SharedPref;

public class MainActivity extends BaseActivity<ActivityMainBinding> implements View.OnClickListener {
    private Fragment[] mFragments = new Fragment[3];
    private View[] mTabViews = new View[3];

    @Override
    public ActivityMainBinding getBinding(LayoutInflater inflater) {
        return ActivityMainBinding.inflate(inflater);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initViews();
        mBinding.ivHomeMaterial.setOnClickListener(this);
        mBinding.ivHomeUtil.setOnClickListener(this);
        mBinding.ivHomeMe.setOnClickListener(this);

        showThisFragment(0);

        Constance.TOKEN = SharedPref.getInstance().getString("token", null);
        if (!TextUtils.isEmpty(Constance.TOKEN)) {
            Constance.isLogin = true;
        }
    }

    private void initViews() {
        mFragments[0] = getFragment(MaterialFragment.class);
        mFragments[1] = getFragment(UtilFragment.class);
        mFragments[2] = getFragment(MeFragment.class);

        mTabViews[0] = mBinding.ivHomeMaterial;
        mTabViews[1] = mBinding.ivHomeUtil;
        mTabViews[2] = mBinding.ivHomeMe;

        if (getSupportFragmentManager().getFragments().isEmpty()) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fcv_home_container, mFragments[0], MaterialFragment.class.getSimpleName())
                    .add(R.id.fcv_home_container, mFragments[1], UtilFragment.class.getSimpleName())
                    .add(R.id.fcv_home_container, mFragments[2], MeFragment.class.getSimpleName())
                    .commitAllowingStateLoss();
        }
    }

    private Fragment getFragment(Class<? extends Fragment> clazz) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(clazz.getSimpleName());
        if (fragment == null) {
            try {
                fragment = clazz.newInstance();
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            }
        }
        return fragment;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_home_material) {
            showThisFragment(0);
        } else if (view.getId() == R.id.iv_home_util) {
            showThisFragment(1);
        } else if (view.getId() == R.id.iv_home_me) {
            showThisFragment(2);
        }
    }

    private void showThisFragment(int index) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < mFragments.length; i++) {
            if (index == i) {
                ft.show(mFragments[index]);
                mTabViews[i].setSelected(true);
            } else {
                ft.hide(mFragments[i]);
                mTabViews[i].setSelected(false);
            }
        }
        ft.commitAllowingStateLoss();
    }
}
